package cn.lentme.app.component.view.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import cn.lentme.app.activity.MainActivity;
import cn.lentme.app.activity.MatchActivity;
import cn.lentme.app.activity.PaperHomeActivity;
import cn.lentme.app.activity.R;
import cn.lentme.app.activity.SplashActivity;
import cn.lentme.app.base.BaseActivity;

public class HomeFragment extends Fragment {

    //组件
    private View view;
    private Button btn_match, btn_paper, btn_active;
    private ViewGroup container;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frag_home, container, false);
        this.container = container;
        Init();
        return view;
    }

    private void Init(){
        //main初始化
        InitView();
    }

    private void InitView(){
        // 匹配按键
        btn_match = view.findViewById(R.id.main_btn_match);
        // 小纸条按键
        btn_paper = view.findViewById(R.id.main_btn_paper);
        btn_active = view.findViewById(R.id.main_btn_activities);

        // 绑定监听器
        btn_match.setOnClickListener(view -> onMatch());
        btn_paper.setOnClickListener(view->onClickPaper());
        btn_active.setOnClickListener(view -> {
            ((MainActivity)getActivity()).setItem(1);
        });
    }

    private void onMatch(){//匹配函数

        if (!SplashActivity.SIGN_NETWORK ){
            if (SplashActivity.checkedNetwork(((BaseActivity)getActivity()))) {
                ((BaseActivity)getActivity()).show("网络连接成功！即将重启app( ￣▽￣)σ");
                gotoSplashActivity();
            } else {
                ((BaseActivity)getActivity()).show("网络没连上啊( ‘-ωก̀ )");
            }
            return;
        }

        gotoChatActivity();
    }

    // 小纸条
    private void onClickPaper(){
        getActivity().startActivity(new Intent(getActivity(), PaperHomeActivity.class));
        ((BaseActivity)getActivity()).animFade();
    }

    private void gotoSplashActivity(){
        ((BaseActivity)getActivity()).startActivity(new Intent(getActivity(),SplashActivity.class));
        ((BaseActivity)getActivity()).animFade();
        ((BaseActivity)getActivity()).finish();
    }

    private void gotoChatActivity(){
        ((BaseActivity)getActivity()).startActivity(new Intent(getActivity(), MatchActivity.class));
        ((BaseActivity)getActivity()).animFade();
    }

}
